import re
import sys
from PanlexDict import PanlexDict

class WBparse:
    """This class is used to parse WB files."""
    #___________________________________________________________________________

    SOURCECODEPOINTS = 31
    TARGETCODEPOINTS = 53
    RECORDSIZE = SOURCECODEPOINTS + TARGETCODEPOINTS

    def __init__(self, infile, outfile, sourceLang, targetLangs, multipleTarget=False, debug=False):
        self._debug = debug
        fpout = open(outfile, "wb")
        codepoints = open(infile).read().decode("utf-8")
        records = PanlexDict()
        currpos = 0
        infilesize = len(codepoints)
        records.sourceLang = sourceLang
        records.targetLangs = targetLangs
        records.langno = 2
        fpout.write(records.writeHeader())
        while currpos < infilesize:
            source = codepoints[currpos:currpos+WBparse.SOURCECODEPOINTS].rstrip("\0")
            source = re.sub("\0.*","",source)
            currpos += WBparse.SOURCECODEPOINTS
            target = codepoints[currpos:currpos+WBparse.TARGETCODEPOINTS].rstrip("\0")
            target = re.sub("\0.*","",target)
            if multipleTarget:
                for targeti in re.split(",",target):
                    fpout.write(records.addSimpleRecord(source,targeti.strip(),retrec=True).encode("utf-8"))
            else:
                fpout.write(records.addRecord(source,target,retrec=True))
            currpos += WBparse.TARGETCODEPOINTS
        fpout.close()


    #___________________________________________________________________________



